package com.xinqi.modules.sys.feedback.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.core.enums.FeedbackModeEnum;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.sys.feedback.domain.FeedbackEntity;
import com.xinqi.modules.sys.feedback.dto.req.FeedbackQueryDTO;
import com.xinqi.modules.sys.feedback.dto.rsp.FeedbackResultDTO;
import com.xinqi.modules.sys.feedback.manager.FeedbackManager;
import com.xinqi.modules.sys.feedback.mapper.FeedbackMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 用户反馈表管理层实现
 *
 * @author: yyzhenn
 * @date: 2023/04/23
 */
@Service
@RequiredArgsConstructor
public class FeedbackManagerImpl extends BaseServiceImpl<FeedbackMapper, FeedbackEntity> implements FeedbackManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<FeedbackResultDTO> findPage(Page<Object> page, FeedbackQueryDTO query) {
        query.setIsDelete(YesNoNumberEnum.NO.getCode());
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 根据id查询 用户反馈表数据
     */
    @Override
    public FeedbackResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("feedback.id.not.blank"));
        return this.baseMapper.findById(id);
    }

    /**
     * 查询用户有奖反馈记录
     *
     * @param userId
     * @return
     */
    @Override
    public FeedbackEntity findRewardedFeedback(Long userId) {
        LambdaQueryWrapper<FeedbackEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FeedbackEntity::getMode, FeedbackModeEnum.REWARDED_FEEDBACK.getCode())
            .eq(FeedbackEntity::getCreateBy, userId)
            .last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
    }

    /**
     * 查询用户有奖反馈记录
     *
     * @param userIds
     * @return
     */
    @Override
    public FeedbackEntity findRewardedFeedbackList(List<Long> userIds) {
        LambdaQueryWrapper<FeedbackEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FeedbackEntity::getMode, FeedbackModeEnum.REWARDED_FEEDBACK.getCode())
            .in(FeedbackEntity::getCreateBy, userIds)
            .last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
    }

}
